//
//  InvestController.m
//  LingQian
//
//  Created by xieyingze on 2018/1/21.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "InvestController.h"
#import "InvestWaveTypeCell.h"
#import "SelectThemeStyleView.h"
#import "InvestViewModel.h"
#import "InvestProgressArcTypeCell.h"

@interface InvestController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) BaseButton *leftButton;
@property (nonatomic, strong) InvestViewModel *viewModel;
@property (nonatomic, strong) SelectThemeStyleView *themeStyleView;

@end

@implementation InvestController

#pragma mark - *********************生命周期*********************

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - *********************基础配置*********************

- (void)configuration {
    self.navigationController.navigationBar.hidden = YES;
    self.navTitle = @"投资";
}

- (void)initSubViews {
    [self.view addSubview:self.tableView];
    [self.navView addSubview:self.leftButton];
}

- (void)autoLayoutForSubViews {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.centerY.equalTo(@10);
        make.width.equalTo(@60);
        make.height.equalTo(@40);
    }];
    [self.view bringSubviewToFront:self.navView];
}

- (void)bindViewModel {
    @weakify(self);
    [[self.leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        NSLog(@"%s",__func__);
        [self showSelectThemeStyleView];
    }];
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kNotifi_InvestRefresh object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
    [self.viewModel.longPressSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        NSLog(@"%s",__func__);
        [self showSelectThemeStyleView];
    }];
}

#pragma mark - *********************基础方法*********************


#pragma mark - *********************代理方法*********************

#pragma mark - UITableViewDataSource
//返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self returnBaseTabelViewCellWithThmeType:[ThemeManager shareInstance].themeStyle tableView:tableView cellForRowAtIndexPath:indexPath];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    CGFloat alpha = y <= 0 ? 0.5 : (y > 320 ? 0.9: (y/320));
    self.navView.mainView.backgroundColor = [kCOLOR_white colorWithAlphaComponent:alpha];
}

- (BaseTableViewCell *)returnBaseTabelViewCellWithThmeType:(kThemeStyle)themeStyle tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (themeStyle == kThemeStyleRed) {
        InvestProgressArcTypeCell *cell = [InvestProgressArcTypeCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        return cell;
    } else {
        InvestWaveTypeCell *cell = [InvestWaveTypeCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s",__func__);
}

#pragma mark - *********************响应事件*********************

- (void)showSelectThemeStyleView {
    @weakify(self);
    self.themeStyleView.selectCompleted = ^(NSInteger index) {
        @strongify(self);
        [self updateUI];
    };
    [self.themeStyleView showSelectThemeStyleView];
}

- (void)updateUI {
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotifi_homeRefresh object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotifi_InvestRefresh object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotifi_meRefresh object:nil];
}

#pragma mark - *********************懒加载*********************

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = kCOLOR_tableView;
        _tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
        _tableView.rowHeight = 130;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (BaseButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [BaseButton new];
        [_leftButton setButtonStyle:@"Style" titleColor:KCOLOR_nornalFont titleFont:kFONT_mainFont(14) texrAlignment:NSTextAlignmentLeft];
    }
    return _leftButton;
}

- (SelectThemeStyleView *)themeStyleView {
    if (!_themeStyleView) {
        _themeStyleView = [SelectThemeStyleView new];
    }
    return _themeStyleView;
}

- (InvestViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [InvestViewModel new];
    }
    return _viewModel;
}

@end
